package com.ruoyi.project.api.controller;

import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.order.domain.BinsOrder;
import com.ruoyi.project.order.service.IBinsOrderService;
import com.ruoyi.project.order.service.date.DateUtil;
import com.ruoyi.project.vehicle.domain.BinsVehicleInfo;
import com.ruoyi.project.vehicle.service.IBinsVehicleInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @title : OrderController
 * @Author : Administrator
 * @Date : 2025/4/10
 * @Version : 1.0.0
 * @Description :
 */
@RestController
@RequestMapping("/api/order")
public class OrderController {

    private Logger logger = LoggerFactory.getLogger(OrderController.class);
    @Autowired
    IBinsOrderService orderService;
    @Autowired
    IBinsVehicleInfoService vehicleService;
    /**
     * 订单列表
     * @param map
     * @return
     */
    @RequestMapping("/list")
    public AjaxResult getOrderList(@RequestBody BinsOrder map){
        logger.info("订单列表请求参数：{}",map);
        List<BinsOrder> list = orderService.selectBinsOrderList(map);
        return AjaxResult.success(list);
    }

    /**
     * 订单列表
     * @param orderNo
     * @return
     */
    @RequestMapping("/info")
    public AjaxResult getOrderInfo(String orderNo){
        logger.info("订单列表请求参数：{}",orderNo);
        BinsOrder orderInfo = orderService.selectBinsOrderByOrderNo(orderNo);
        return AjaxResult.success(orderInfo);
    }

    /**
     * 订单列表
     * @param orderInfo
     * @return
     */
    @RequestMapping("/create")
    public AjaxResult createOrderInfo(@RequestBody BinsOrder orderInfo){
        logger.info("订单创建请求参数：{}",orderInfo);
        orderInfo.setOrderStatus(1);
        orderInfo.setOrderPrice(new BigDecimal("0.00"));
        int count = orderService.insertBinsOrder(orderInfo);
        return AjaxResult.success(orderInfo);
    }


    /**
     * 订单结束行程
     * @param orderInfo
     * @return
     */
    @RequestMapping("/over")
    public AjaxResult overOrderInfo(@RequestBody BinsOrder orderInfo){
        logger.info("订单创建请求参数：{}",orderInfo);
        BinsOrder orderInfo_data = orderService.selectBinsOrderByOrderNo(orderInfo.getOrderNo());
        if (orderInfo_data==null){
            return AjaxResult.error("订单不存在");
        }
        if (orderInfo_data.getOrderStatus()>=2){
            return AjaxResult.success("结束行程失败", orderService.selectBinsOrderByOrderNo(orderInfo.getOrderNo()));
        }
        BinsVehicleInfo  binsVehicleInfo= vehicleService.selectBinsVehicleInfoByVehicleCode(orderInfo_data.getVehicleCode());
        orderInfo_data.setOrderStatus(2);
        orderInfo_data.setEndTime(DateUtil.getCurrentTime(DateUtil.YYYY_MM_DD_HH_MM_SS));
        if (binsVehicleInfo!=null) {
            orderInfo_data.setDestination(binsVehicleInfo.getAddress());
            BinsVehicleInfo vehicleInfo = new BinsVehicleInfo();
            vehicleInfo.setVehicleCode(binsVehicleInfo.getVehicleCode());
            vehicleInfo.setAddress("未知");
            vehicleInfo.setLockFlag(1);
            vehicleInfo.setWorkStatus("2");
            vehicleInfo.setWorkUser("");
            vehicleService.updateBinsVehicleInfo(vehicleInfo);
        }
        if (orderInfo_data.getDestination()==null || "".equals(orderInfo_data.getDestination())){
            orderInfo_data.setDestination("未知");
        }
        int count = orderService.updateBinsOrder(orderInfo_data);
        if (count == 0){
            return AjaxResult.error("结束行程失败", orderService.selectBinsOrderByOrderNo(orderInfo.getOrderNo()));
        }
        orderInfo = orderService.payOrder(orderInfo_data.getOrderNo(), "0");
        return AjaxResult.success(orderInfo);
    }
}
